/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
public class ListBankQueue <E> extends AbstractQueue{

    public Node<E> tail;

    public ListBankQueue(int s){
        tail = null;
        this.count = 0;
        this.size = s;
    }

    public void queue(Object item) throws QueueOverflow{
        QueueOverflow error = new QueueOverflow("Queue Overflow");
        Node<E> temp = new Node(item);

        if (!this.getFull()){
            if (tail == null){
                tail = temp;
                tail.setNext(tail);
            }
            else{
                temp.setNext(tail.getNext());
                tail.setNext(temp);
            }
            this.count++;
        }
        else
            throw error;

    }

    public E dequeue() throws QueueUnderflow{
        QueueUnderflow error = new QueueUnderflow("Queue Underflow");
        Node<E> finger = tail;
        if (!this.getEmpty()){
            while(finger.getNext() != tail)
                finger = finger.getNext();
            Node<E> temp = tail;
            if(finger == tail)
                tail = null;
            else{
                finger.setNext(tail.getNext());
                tail = finger;
            }
            this.count--;
            return((E)temp.getValue());
        }
        else
            throw error;
        
    }

    public E peek() throws QueueUnderflow{
        QueueUnderflow error = new QueueUnderflow("Queue Underflow");
        Node<E> finger = tail;
        
        if (!this.getEmpty()){
            while(finger != tail)
                finger = finger.getNext();
            return((E)finger.getValue());    
        }
        else
            throw error;
    }

    public int getCount(){
        return(this.count);
    }
}
